package com.ptkc.ext;

import com.alibaba.fastjson.JSON;
import com.ptkc.core.canal.handler.EventTypeHandler;
import com.ptkc.core.esearch.model.Activities;
import com.ptkc.core.esearch.repos.ActivitiesRepository;
import com.ptkc.core.esearch.repos.BusinessRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @author: Mr.Jiao
 * @date: 2018年12月06日
 */
@Slf4j
@Component
public class ActivitiesCanalHandler extends EventTypeHandler<Activities> {

    @Autowired
    private ActivitiesRepository activitiesRepository;

//    @Autowired
//    private BusinessRepository businessRepository;

    @Override
    public Class<? extends Activities> getEntryClass() {
        return Activities.class;
    }

    @Override
    public boolean getSubscribe(String database, String table) {
        return this.getDatabase().equalsIgnoreCase(database) && this.getTable().equalsIgnoreCase(table);
    }

    public ActivitiesCanalHandler(){
        super("runfast", "runfast_activity");
    }


    @Override
    public void onDelete(Activities rowData) {
        if(rowData != null) {
            try {
                activitiesRepository.delete(rowData);
//                businessRepository.delActivityTargetByActivityId(rowData.getId());
            } catch (IOException e) {
                e.printStackTrace();
                log.error("删除订单信息失败:{}", JSON.toJSONString(rowData));
            }
        }
    }

    @Override
    public void onInsert(Activities rowData) {
        this.onUpdate(rowData);
    }

    @Override
    public void onUpdate(Activities rowData) {
        if(rowData != null) {
            try {
                activitiesRepository.save(rowData);
//                if(rowData.getDiscard() != null && rowData.getDiscard()){
//                    businessRepository.delActivityTargetByActivityId(rowData.getId());
//                }else if(rowData.getDeleted() != null && rowData.getDeleted().equals(1)){
//                    businessRepository.delActivityTargetByActivityId(rowData.getId());
//                }
            } catch (IOException e) {
                e.printStackTrace();
                log.error("保存订单信息失败:{}", JSON.toJSONString(rowData));
            }
        }
    }



}
